package com.zx.sa.controller;

import com.zx.sa.common.ModelType;
import com.zx.sa.service.EmbeddingService;
import jakarta.annotation.Resource;
import java.io.IOException;
import java.util.List;
import org.springframework.ai.document.Document;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/embd")
public class EmbeddingController {

    @Resource
    private EmbeddingService embeddingService;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("models", ModelType.getModels());
        return "embeding";
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        try {
            embeddingService.uploadFile(file);
            return "File uploaded successfully";
        } catch (IOException e) {
            return "Error uploading file: " + e.getMessage();
        }
    }


    @GetMapping("/search")
    @ResponseBody
    public List<Document> search(@RequestParam("query") String query) {
        return embeddingService.searchData(query, 3);
    }

} 